package com.android.myapplication.view.media;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.myapplication.R;

public class SoundPlayActivity extends Activity {
    String[] soundList = new String[]{"Barium.ogg","Carbon.ogg","Cesium.ogg","Fermium.ogg","Hassium.ogg","Helium.ogg","Krypton.ogg"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);
        ListView listView = findViewById(R.id.list);

        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, soundList));
        final SoundPool mSoundPlayer = new SoundPool(10,
                AudioManager.STREAM_MUSIC, 5);

        int streamID = mSoundPlayer.load(this, R.raw.barium, 1);// 1
        mSoundPlayer.load(this, R.raw.carbon, 1);// 2
        mSoundPlayer.load(this, R.raw.cesium, 1);// 3
        mSoundPlayer.load(this, R.raw.fermium, 1);// 4
        mSoundPlayer.load(this, R.raw.hassium, 1);// 5
        mSoundPlayer.load(this, R.raw.helium, 1);// 6
        mSoundPlayer.load(this, R.raw.krypton, 1);// 7
        mSoundPlayer.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                //等待对话框
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (view.getTag() != null){
                    int streamID = (Integer) view.getTag();
                    mSoundPlayer.stop(streamID);
                }else {
                    int streamID = mSoundPlayer.play(position + 1, 1, 1, 0, -1, 2f);
                    view.setTag(streamID);
                }

            }
        });
        findViewById(R.id.btn_pause).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSoundPlayer.autoPause();
            }
        });
    }
}
